﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DBUtility;

namespace CUREDataAccessLayers
{
    public class LoginPDA
    {
        /// <summary>
        /// 根据扫描枪扫描的数据判断是否存在此用户
        /// </summary>
        /// <param name="userid">用户条码</param>
        /// <returns>true存在此用户,false不存在此用户</returns>
        public bool JudgeUserISExistById(string userid)
        {
            bool result = false;
            DataTable dt = new DataTable();
            StringBuilder sql = new StringBuilder();
            sql.Append("select userid from master t where t.userid='" + userid + "'");
            try
            {
                dt = DbHelperOra.QueryTable(sql.ToString());
                //this.DbHelperInstance.Fill(dt, sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 根据用户手动输入判断是否存在此用户
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">用户密码</param>
        /// <returns>true存在此用户,false不存在此用户</returns>
        public bool JudgeUserISExistByName(string username, string password)
        {
            bool result = false;
            DataTable dt = new DataTable();
            StringBuilder sql = new StringBuilder();
            sql.Append("select name from master t where  t.name='" + username + "' and t.password='" + password + "' ");
            try
            {
                dt = DbHelperOra.QueryTable(sql.ToString());
                //this.DbHelperInstance.Fill(dt, sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 锁定用户
        /// </summary>
        /// <param name="flag">贬值</param>
        /// <param name="user"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public bool LockMaster(string flag, string user, string action)
        {
            bool result = false;
            int userid = GetOperaterByUseridOrUsername(flag, user);
            if (userid != -1)
            {
                StringBuilder sql = new StringBuilder();
                sql.Append("insert into masterlock values('" + userid + "','" + action + "','0','','" + DateTime.Now.ToString("yyyyMMddHHmmss", System.Globalization.DateTimeFormatInfo.InvariantInfo) + "','','0')");
                try
                {
                    int i = DbHelperOra.ExecuteSql(sql.ToString());
                    //int i = this.DbHelperInstance.ExecuteNonQuery(CommandType.Text, sql);
                    if (i > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return result;
        }

        public bool UnLockMaster(string flag, string user, string usernum)
        {
            bool result = false;
            StringBuilder sql = new StringBuilder();
            int userid = GetOperaterByUseridOrUsername(flag, user);
            string action = GetPDAAuthority(usernum);
            if (action != null && action.ToString() != "")
            {
                if (userid != -1)
                {
                    try
                    {
                        sql.Append(" update masterlock t set t.unlockid='" + usernum + "',t.unlocktime='" + DateTime.Now.ToString("yyyyMMddHHmmss", System.Globalization.DateTimeFormatInfo.InvariantInfo) + "',t.flag='1',t.status='1' where t.id='" + userid + "'");
                        int i = DbHelperOra.ExecuteSql(sql.ToString());
                        //int i = this.DbHelperInstance.ExecuteNonQuery(CommandType.Text, sql);
                        if (i > 0)
                        {
                            result = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
            return result;
        }


        /// <summary>
        /// 根据用户条码或者用户名获取出用户ID
        /// </summary>
        /// <param name="flag">区分是用户条码还是用户名</param>
        /// <param name="user">用户</param>
        /// <returns>用户ID</returns>
        public int GetOperaterByUseridOrUsername(string flag, string user)
        {
            int operater = -1;
            StringBuilder sql = new StringBuilder();
            if (flag == "1")//员工号登录
            {
                sql.Append("select t.id from master t where t.userid='" + user + "'");
            }
            if (flag == "2")//用户名密码登录
            {
                sql.Append("select t.id from master t where t.name='" + user + "'");
            }
            try
            {
                Object obj = DbHelperOra.GetSingle(sql.ToString());
                if (!Object.Equals(obj, null))
                {
                    operater = Convert.ToInt32(obj);
                }
                //operater = (int)this.DbHelperInstance.ExecuteScalar<decimal>(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return operater;
        }


        /// <summary>
        /// 根据用户条码获取出用户ID
        /// </summary>
        /// <param name="flag">区分是用户条码还是用户名</param>
        /// <param name="user">用户</param>
        /// <returns>用户ID</returns>
        public string GetOperaterByUserid(string user)
        {
            string groupid = string.Empty;
            StringBuilder sql = new StringBuilder();
            sql.Append("select  m.group_id from master t,mastergroup m where m.master_id=t.id and t.userid='" + user + "'");
            try
            {
                Object obj = DbHelperOra.GetSingle(sql.ToString());
                if (!Object.Equals(obj, null))
                {
                    groupid = obj.ToString();
                }
                //groupid = this.DbHelperInstance.ExecuteScalar<decimal>(sql).ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return groupid;
        }



        /// <summary>
        /// PDA根据当前用户及模块信息判断是否有进入此模块的权限
        /// </summary>
        /// <param name="flag">区分是用户条码还是用户名</param>
        /// <param name="user">用户</param>
        /// <param name="action">模块</param>
        /// <returns></returns>
        public string GetPDAAuthority(string user)
        {
            string result = string.Empty;
            StringBuilder sql = new StringBuilder();
            DataTable dt = new DataTable();
            string groupid = GetOperaterByUserid(user);
            if (groupid != null && groupid != "")
            {
                sql.Append("select * from action t,actiongroup g where t.action(+)=g.action and t.action_columnid='0' and group_id='" + groupid + "' and t.action='UNLOCK'");
                try
                {
                    dt = DbHelperOra.QueryTable(sql.ToString());
                    //this.DbHelperInstance.Fill(dt, sql);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            result += dt.Rows[i]["action"].ToString() + ",";
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return result;
        }


        /// <summary>
        /// 查询此用户是否被锁定
        /// </summary>
        /// <param name="flag"></param>
        /// <param name="user"></param>
        /// <returns>0初始状态1被锁定2没锁定</returns>
        public int SelectUserHasAuthority(string flag, string user)
        {
            int result = 0;
            int userid = GetOperaterByUseridOrUsername(flag, user);
            StringBuilder sql = new StringBuilder();
            DataTable dt = new DataTable();
            sql.Append("select * from masterlock t where t.id='" + userid + "' and t.flag='0' and t.status='0'");
            try
            {
                dt = DbHelperOra.QueryTable(sql.ToString());
                //this.DbHelperInstance.Fill(dt, sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    result = 1;
                }
                else
                {
                    result = 2;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
    }
}
